import React, { Component, PropTypes } from 'react';
import FormElement from '../Form/FormElement'

class Form extends Component {

  static propTypes = {
    columns: React.PropTypes.array.isRequired,
    data: React.PropTypes.object,
    onChange: React.PropTypes.func
  }

  render() {
    let columns = $.extend(true, [], this.props.columns)
    columns.slice(-1)[0].command && columns.pop()
    let data = this.props.data

    return (
      <form className="form-horizontal" onChange={this.props.onChange} data-id={data.id}>
        {columns.map(config => {
          return <FormElement config={config} value={data[config.name]} key={Math.random()} />
        })}
      </form>
    )
  }

}

export default Form

